<?php
function delete_struktur($sid){
	$query = "DELETE FROM pr_struktur_gaji_tahun WHERE struktur_gaji_id={$sid}";
	$query_detail = "DELETE FROM struktur_gaji_detail WHERE struktur_gaji_id={$sid}";
	my_query($query_detail);
	return my_query($query);
}
function list_tahun_struktur(){
	my_set_code_js('
		function confirmDelete( i){
		var check = confirm(\'Akan mendelete data? \'); 
		if(check){
			location.href=\'index.php?com='.$_GET['com'].'&task=delete&sid=\'+i;
		}
		return;	
		}
	');
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nomor SK'=>array('style'=>'border-bottom:2px solid;width:40%'),  
		'Periode'=>array('style'=>'border-bottom:2px solid;width:40%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:15%'), 
	);
	$query = "SELECT * FROM pr_struktur_gaji_tahun ";
	$result = my_query($query);
	$row = array();
	$i=0;
	while( $ey = my_fetch_array($result) ){
		$i++;
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=struktur_detail&sid='.$ey['struktur_gaji_id'],
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$editp = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=edit_tahun&id='.$ey['struktur_gaji_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editp  );
	
		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['struktur_gaji_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nomor'=>  $ey['nomor_sk'] ,   
			'periode'	=> position_text_align ( date("m-Y", strtotime($ey['tanggal'] ." 00:00:00" ) ), 'left'), 
			'operasi'=> position_text_align(  $edit_button.' '. $detail_button .' '.$delete_button, 'right'),  
		); 
	}
	
	$datas = table_rows($row); 
	return table_builder($header , $datas ,  5 , false );
}


function add_tahun($id = 0){
	
	$view = form_header( "tahun" , "tahun"  );
	if($id>0){
		$fields = my_get_data_by_id( 'pr_struktur_gaji_tahun' , 'struktur_gaji_id' , $id );
		$month = date('m',strtotime($fields['tanggal']));
		$year = date('Y',strtotime($fields['tanggal']));	
	}else{
		$month = date('m');
		$year = date('Y');
	} 

	$nomor_sk = array(
			'name'=>'nomor_sk',
			'value'=>(isset($_POST['nomor_sk'])? $_POST['nomor_sk'] : $fields['nomor_sk']),
			'id'=>'nomor_sk',
			'type'=>'textfield',
			'size'=>'35'
		);
	$form_nomor = form_dynamic($nomor_sk);
	$view .= form_field_display( $form_nomor  , "Nomor SK"  );
	
	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$bulan = array(
		'name'=>'bulan',
		'value'=>isset($_POST['bulan']) ? $_POST['bulan']: $month  
	);
	$bulan_form = form_dropdown( $bulan,$bulans);
	
	$tahun = array(
		'name'=>'tahun','size'=>5,
		'value'=>isset($_POST['tahun']) ? $_POST['tahun']: $year
	); 
	$tahun_form = form_dynamic($tahun );

	$view .= form_field_display( $bulan_form.' '. $tahun_form  , "Periode"    );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function struktur_gaji_detail($struktur_gaji_id){
	my_set_code_js('
	function confirmDelete(id){
		var t = confirm(\'Yakin akan menghapus data ?\');
		if(t){
			location.href=\'index.php?com=struktur_gaji&task=delete_detail&id=\'+id;
		}
		return false;
	}
	');
	$header = array(
		'Grade'=>array('style'=>'text-align:center;border-bottom:2px solid;width:6%'),  
		'Min'=>array('style'=>'text-align:right;border-bottom:2px solid;width:28%'),  
		'Mid'=>array('style'=>'text-align:right;border-bottom:2px solid;width:28%'),  
		'Max'=>array('style'=>'text-align:right;border-bottom:2px solid;width:28%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	
	$query = "SELECT * FROM struktur_gaji_detail a
		INNER JOIN grade b ON b.grade_id=a.grade_id 
		WHERE a.struktur_gaji_id=".$struktur_gaji_id. 
		" ORDER BY a.grade_id ASC ";
	$result = my_query($query);
	$row = array();
	while( $ey = my_fetch_array($result) ){
	
		$editproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=edit_data&id='.$ey['struktur_gaji_detail_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$temp = my_fetch_array(my_query("SELECT MAX(grade_id) as max FROM struktur_gaji_detail WHERE struktur_gaji_id=".$struktur_gaji_id)) ;
		
		$max_grade=$temp['max'];
		if($ey['grade_id'] < $temp['max'] ){		
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{		
			$deleteproperty = array(
				'href'=>'javascript:; ',
				'onclick'=>'javascript:confirmDelete('.$ey['struktur_gaji_detail_id'].');',
				'title'=>'Delete'
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );		
		}	
		//$data_rec = get_data_value($ey['grade_id'] , $struktur_gaji_id);
		
		$row[] = array( 
			'grade'=> position_text_align(  $ey['grade_id'],'center' ),   
			'min'=>  position_text_align( rp_format($ey['min']),'right' ),   
			'mid'=>  position_text_align( rp_format($ey['mid']),'right' ),   
			'max'=>  position_text_align( rp_format($ey['max']),'right' ),   
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		); 
	}
	
	$datas = table_rows($row); 
	$view = form_header( "..." , "..."  );
	$sks=my_get_data_by_id('pr_struktur_gaji_tahun','struktur_gaji_id',$struktur_gaji_id);
	$view .= form_field_display( $sks['nomor_sk']   , "Nomor SK"   );
	$view .= form_field_display( date("m-Y", strtotime($sks['tanggal']))   , "Periode"   );
	$view .= form_footer( );
	return  $view.table_builder( 	$header , 	$datas , 	5 , 	false );
}


function delete_detail($id){
	$query = "DELETE FROM struktur_gaji_detail WHERE struktur_gaji_detail_id={$id}";
	return my_query($query);
}

function add_data($id, $struktur_tahun_id){
	$view = form_header( "..." , "..."  );
	$fields = my_get_data_by_id('struktur_gaji_detail','struktur_gaji_detail_id',$id);

	if($id==0){
		$query = "SELECT * FROM grade  ORDER BY grade_id ASC";
		// ORDER BY kode_grade ASC
		$result = my_query($query);
		$grades = array();
		while( $r = my_fetch_array($result) ){
			$grades[$r['grade_id']] =$r['kode_grade'];
		}

		$grade= array(
			'name'=>'grade_id',
			'value'=>( isset($_POST['grade_id'] ) ? $_POST['grade_id'] : $fields['grade_id']  ),
			'id'=>'grade_id'  
		);
		$form_grade = form_dropdown($grade , $grades );	
		$view .= form_field_display( $form_grade , "Grade " );
	}else{
		$view .= form_field_display( $fields['grade_id']   , "Grade"   );	
	}
	
	$min = array(
				'name'=>'min',
				'value'=>( isset($_POST['min']) ? $_POST['min'] : rp_format($fields['min'] )),
				'id'=>'min',
				'type'=>'text',
				'size'=>'35'
			);
	$form_field_min = form_money($min);
	$view .= form_field_display( $form_field_min   , "Minimum"   );
	$mid = array(
				'name'=>'mid',
				'value'=>( isset($_POST['mid']) ? $_POST['mid'] : rp_format($fields['mid']) ),
				'id'=>'mid',
				'type'=>'text',
				'size'=>'35'
			);
	$form_field_mid = form_money($mid); 
	$view .= form_field_display( $form_field_mid   , "Middle"   );
	
	$max = array(
				'name'=>'max',
				'value'=>( isset($_POST['max']) ? $_POST['max'] : rp_format($fields['max']) ),
				'id'=>'max',
				'type'=>'text',
				'size'=>'35'
			);
	$form_field_max = form_money($max); 
	$view .= form_field_display( $form_field_max   , "Maximum"   );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;

}

function add_data_validate($id, $sid ){
	$errsubmit = false;
	$err = array();
	
	if($sid==0){
		$code =  trim($_POST['grade_id']); 
		if( $id==0 AND $code == ''){
			$errsubmit = true;
			$err[] = "Grade belum di isi";
		}
		$query="SELECT grade_id FROM struktur_gaji_detail
			WHERE grade_id='".$code.
			"' AND struktur_gaji_detail_id<>".$id; 
		if (my_num_rows( my_query($query)) >0){
			$errsubmit = true;
			$err[] = "Grade sudah digunakan";	
		}
	}	
	
	if(set_text_as_number($_POST["min"]) > set_text_as_number($_POST["mid"])){
		$errsubmit = true;
		$err[] = "Minimum tidak boleh lebih besar dari Middle";
	}
	if(set_text_as_number($_POST["min"]) > set_text_as_number($_POST["max"])){
		$errsubmit = true;
		$err[] = "Minimum tidak boleh lebih besar dari Maximum";
	}	
	if(set_text_as_number($_POST["mid"]) > set_text_as_number($_POST["max"])){
		$errsubmit = true;
		$err[] = "Middle tidak boleh lebih besar dari Maximum";
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}


function add_tahun_submit($id =0){
	$tanggal = $_POST['tahun'].'-'.$_POST['bulan'].'-01';
	
	$datas = array(
		'nomor_sk'=>my_type_data_str($_POST['nomor_sk']),
		'tanggal'=> my_type_data_str($tanggal),
		'datetime_added'=>my_type_data_function('NOW()') 		
	);
	
	if($id > 0){
		return my_update_record( 'pr_struktur_gaji_tahun' , 'struktur_gaji_id' , $id , $datas);
	}
	
	return my_insert_record( 'pr_struktur_gaji_tahun' , $datas );
}

function add_data_submit( $id , $struktur_tahun_id ){
	if($id>0){
		$datas = array(
			'min'=>my_type_data_str($_POST['min']),
			'mid'=>my_type_data_str($_POST['mid']),
			'max'=>my_type_data_str($_POST['max']),
		);	
		return my_update_record( 'struktur_gaji_detail' , 'struktur_gaji_detail_id' , $id , $datas);
	}
	$datas = array(
		'grade_id'=>my_type_data_int($_POST['grade_id']),
		'struktur_gaji_id'=>my_type_data_int($struktur_tahun_id),
		'min'=>my_type_data_str(set_text_as_number($_POST['min'])),
		'mid'=>my_type_data_str(set_text_as_number($_POST['mid'])),
		'max'=>my_type_data_str(set_text_as_number($_POST['max'])),
	);	
	return my_insert_record( 'struktur_gaji_detail' , $datas );
}

function auto_grade( $sid ){

		$query = "SELECT * FROM grade  ORDER BY grade_id ASC";
		$result = my_query($query);
		while( $r = my_fetch_array($result) ){
			$datas = array(
				'grade_id'=>my_type_data_int($r['grade_id']),
				'struktur_gaji_id'=>my_type_data_int($sid),
			);
			$res = my_query("SELECT struktur_gaji_detail_id FROM 
				struktur_gaji_detail 
				WHERE grade_id=".$r['grade_id']." 
				AND struktur_gaji_id=".$sid); 
			if(	my_num_rows($res) == 0){
				my_insert_record( 'struktur_gaji_detail' , $datas );
			}	
		}
	return true;
}

function add_tahun_validate($id){
	$errsubmit = false;
	$err = array();
	
	$code =  trim($_POST['nomor_sk']); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Nomor SK belum di isi";
	}
	
	$query="SELECT struktur_gaji_id FROM pr_struktur_gaji_tahun
		WHERE nomor_sk='".$code.
		"' AND struktur_gaji_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nomor SK sudah digunakan";	
	}
	
	if( (int)$_POST['bulan'] == 0 ){
		$errsubmit = true;
		$err[] = "Bulan belum di isi";	
	}
	
	if( (int)$_POST['tahun'] == 0 ){
		$errsubmit = true;
		$err[] = "Tahun belum di isi";	
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}